﻿#region Using

using System;
using System.Diagnostics;

#endregion Using

namespace T4_Eind_programma
{
    public class ItemInfo
    {
        #region Variables

        private bool active;
        private Stopwatch activeTimer = new Stopwatch();
        private double effectiviteit = 0;
        private DateTime elapsedTime;
        private string filteredName;
        private object itemValue;
        private string name;

        #endregion Variables

        #region Constructor

        public ItemInfo(string name, object value)
        {
            this.name = name;
            this.itemValue = value;
            filteredName = FilterName(name);
        }

        public ItemInfo(string name)
        {
            this.name = name;
            filteredName = FilterName(name);
        }

        #endregion Constructor

        #region Properties

        public bool Active
        {
            get
            {
                return active;
            }
            set
            {
                active = value;
                if (active == true)
                {
                    if (activeTimer.IsRunning == false)
                        activeTimer.Start();
                }
                else
                {
                    if (activeTimer.IsRunning == true)
                        activeTimer.Stop();
                }
            }
        }

        public double Effectiviteit
        {
            get { return effectiviteit; }
            set { effectiviteit = value; }
        }

        public DateTime ElapsedTime
        {
            get { return elapsedTime; }
            set { elapsedTime = value; }
        }

        public string FilteredName
        {
            get
            {
                return filteredName;
            }
            set
            {
                filteredName = value;
            }
        }

        public object ItemValue
        {
            get
            {
                return this.itemValue;
            }
            set
            {
                this.itemValue = value;
                if (itemValue is Boolean)
                {
                    if (Convert.ToBoolean(itemValue) == true)
                    {
                        Active = true;
                    }
                    else
                    {
                        Active = false;
                    }
                }
            }
        }

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public Stopwatch ActiveTimer
        {
            get { return activeTimer; }
            set { activeTimer = value; }
        }

        #endregion Properties

        #region Methodes

        public string BerekenEffectiviteit(DateTime programActiveTimer)
        {
            var timespan = new TimeSpan(programActiveTimer.Ticks);
            if (programActiveTimer.Second > 0)
            {
                effectiviteit = (activeTimer.ElapsedTicks / timespan.Ticks) * 100;
            }
            else
                effectiviteit = 0;

            return effectiviteit + "%";
        }

        public string BerekenEffectiviteit(TimeSpan uitvoerActive)
        {
            if (uitvoerActive.TotalMilliseconds > 0)
            {
                if (activeTimer.ElapsedMilliseconds > 0)
                {
                    effectiviteit = (activeTimer.ElapsedMilliseconds / uitvoerActive.TotalMilliseconds) * 100;
                    if (effectiviteit > 100)
                    {
                        effectiviteit = 100;
                    }
                }
                else
                {
                    effectiviteit = 0;
                }
            }
            else
            {
                effectiviteit = 0;
            }

            return Convert.ToInt32(effectiviteit).ToString() + "%";
        }

        private string FilterName(string name)
        {
            string[] splitter = name.Split('>');
            splitter[splitter.Length - 1].Replace(" ", string.Empty);
            return splitter[splitter.Length - 1];
        }

        #endregion Methodes
    }
}